const Mock = require('mockjs')
var { successData, errorData } = require('../serverData')
var MockRandom = Mock.Random;
var pageCount = MockRandom.integer(1, 10);
var haseMore = true;//是否结束分页
var ids = 10000;//自增长id
var bookListTemplate = {};//数据模板
const users = (req, res) => {
    var currPage = req.query.currPage || 1;
    ids = currPage * 10000;
    let avatar = MockRandom.image('100x100','@color()','#FFF','@character')
    if (currPage === pageCount) {
        haseMore = false;
        bookListTemplate = {
            'hasMore': false,
            'totalPage': pageCount,
            'list|1-10': [//最后一页的数据在1-10的区间产生
                {
                    'id|+1': ids,
                    'name': '@cname',
                    'avatar':avatar,
                    'age': '@natural( 20, 50 )',
                    'address':'@county(true)',
                    'mailbox': '@email()',
                    'createTime': '@datetime()'
                }
            ]
        }
    } else {
        bookListTemplate = {
            'hasMore': true,
            'totalPage': pageCount,
            'list|10': [//有分页的时候一页10条数据
                {
                    'id|+1': ids,
                    'name': '@cname',
                    'avatar':avatar,
                    'age': '@natural( 20, 50 )',
                    'address':'@county(true)',
                    'mailbox': '@email()',
                    'createTime': '@datetime()'
                }
            ]
        }
    }
    var mockData=Mock.mock(bookListTemplate);
    successData.data = mockData
    res.end(JSON.stringify(successData));
}

module.exports = users